import * as XLSX from 'xlsx'
import * as exceljs from 'exceljs/dist/es5/exceljs.browser';

interface wbOpts {
  name?: string,
  type?: string
}

interface Iexport {
  exportData(data: any[], opts?: wbOpts): void
}

class BfExport implements Iexport {
  exportData(data: any[], opts?: wbOpts) {
    let wb = XLSX.utils.book_new();
    let ws = XLSX.utils.json_to_sheet(data);

    /* add worksheet to workbook */
    XLSX.utils.book_append_sheet(wb, ws);

    /* write workbook */
    let filename = `${opts ? opts.name : Date.now()}.${opts ? opts.type : 'xlsx'}`;
    XLSX.writeFile(wb, filename);
  }

  downloadFile(url, filename, mimetype) {
    let ajax = new XMLHttpRequest();
    ajax.open("GET", url, true);
    ajax.responseType = 'blob';
    ajax.onload = (e) => {
      let target: any = e.target;
      let response: any = target.response;
      this.saveAs(filename, response, mimetype);
    };
    ajax.send();
  }

  saveAs(filename, data, mimetype?: any) {
    let defaultMime = "application/octet-stream";
    let blob = new Blob([data], {type: mimetype || defaultMime});
    if (window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveBlob(blob, filename);
    }
    else {
      let elem = window.document.createElement('a');
      elem.href = window.URL.createObjectURL(blob);
      elem.download = filename;
      document.body.appendChild(elem);
      elem.click();
      document.body.removeChild(elem);
    }
  }

  default_user() {
    return ""
  }

  exceljsExport(dtdata: any) {
    let workbook = new exceljs.Workbook();
    let worksheet = workbook.addWorksheet('bfdx');
    worksheet.columns = dtdata.header.map((v, i) => {
      return {
        header: v,
        key: v
      }
    });

    // Add a couple of Rows by key-value, after the last current row, using the column keys
    // worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7)});
    // Add a row by contiguous Array (assign to columns A, B & C)
    // worksheet.addRow([3, 'Sam', new Date()]);

    // Add an array of rows
    worksheet.addRows(dtdata.body);

    worksheet.getCell('A2').font = {
      name: 'Arial Black',
      color: {argb: 'FF00FF00'},
      family: 2,
      size: 14,
      italic: true
    };
    worksheet.getCell('A3').fill = {
      type: 'gradient',
      gradient: 'angle',
      degree: 0,
      stops: [
        {position: 0, color: {argb: 'FF0000FF'}},
        {position: 0.5, color: {argb: 'FFFFFFFF'}},
        {position: 1, color: {argb: 'FF0000FF'}}
      ]
    };
    // add image to workbook by base64
    let imageId = workbook.addImage({
      base64: this.default_user(),
      extension: 'png',
    });
    worksheet.addImage(imageId, 'D1:D1');

    workbook.xlsx.writeBuffer().then(data => {
      const blob = new Blob([data], {type: "application/octet-stream"});
      var elem = window.document.createElement('a');
      elem.href = window.URL.createObjectURL(blob);
      elem.download = `bfdx_${Date.now()}.xlsx`;
      document.body.appendChild(elem);
      elem.click();
      document.body.removeChild(elem);
    });
  }
}

export default new BfExport();
